Problem
Cowmpany Cowmpensation
Description
Allen, having graduated from the MOO Institute of Techcowlogy (MIT), has started a startup! Allen is the president of his startup. He also hires other employees, each of which is assigned a direct superior. If is a superior of and is a superior of then also is a superior of . Additionally, there are no and such that is the superior of and is the superior of . Allen himself has no superior. Allen is employee number , and the others are employee numbers through .
Finally, Allen must assign salaries to each employee in the company including himself. Due to budget constraints, each employee’s salary is an integer between and . Additionally, no employee can make strictly more than his superior.
Help Allen find the number of ways to assign salaries. As this number may be large, output it modulo .
Input
The first line of the input contains two integers and (, ).
The remaining lines each contain a single positive integer, where the line contains the integer (). denotes the direct superior of employee .
Output
Output a single integer: the number of ways to assign salaries modulo .
Example
Input #1
1 | 3 2 |
Output #1
1 | 5 |
Input #2
1 | 3 3 |
Output #2
1 | 10 |
Input #3
1 | 2 5 |
Output #3
1 | 15 |
Note
In the first sample case, employee and report directly to Allen. The three salaries, in order, can be , , , or .
In the second sample case, employee reports to Allen and employee reports to employee . In order, the possible salaries are , , , , , , , , , .
标签:树形DP
多项式插值
Translation
题目大意:给定一棵根为的树,需要给每个结点一个权值,使得父亲的权值不小于儿子的权值,并且所有权值均不大于。求可行方案数并输出其模的值。
Solution
不考虑数据范围,可以先想到一个的暴力做法。
令表示对于结点及其子树,的权值为时可行方案数,那么有转移方程:
用前缀和优化一下,令,那么有
最终答案为,显然复杂度会。
发现一个性质:令的子树大小为,将作为两个多项式看待,那么是一个次多项式,是一个次多项式。
这个性质可以用归纳法证明:
- 时,即为叶子结点时,,,满足性质
- 时,假设该性质对于均成立,那么对于的儿子,为一个次多项式,为一个次多项式。由于,可知为一个次多项式,即次多项式。对于每一个,都对应一个为一个次多项式,于是共有个点值,对应一个次多项式,即为一个次多项式。
由此可知,为一个次多项式,只需求得即可确定此多项式,而所求为,运用特殊多项式在整点上线性插值的方法可以在时间内求得答案。总复杂度。
Code
1 |
|